package com.wordpress.minhnvt1;

import com.wordpress.minhnvt1.objects.May;
import com.wordpress.minhnvt1.objects.NguoiChoi;
import com.wordpress.minhnvt1.objects.OCo;
import com.wordpress.minhnvt1.tools.AudioClip;

import java.util.ArrayList;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.MotionEvent;

public class OAnQuanGame extends ArcadeGame {

	
	// lop luu tru thong tin cho moi lan rai
		public class thongTinCapNhat {
			int viTri;
			int soHat;
			int trangThai;

			public thongTinCapNhat(int ViTri, int SoHat, int TrangThai) {
				this.viTri = ViTri;
				this.soHat = SoHat;
				this.trangThai = TrangThai;

			}

			public thongTinCapNhat() {
			}

		}

		// Refresh rate (ms)
		private static final long UPDATE_DELAY = 300;

		// context;
		private Context mContext;
		// For text

		private Paint mDiemPaint = new Paint();
		private Paint mDiemNguoiChoiPaint = new Paint();
		// For Bitmaps
		private Paint mBitmapPaint = new Paint();

		/**
		 * Constructor
		 * 
		 * @param context
		 */
		public OAnQuanGame(Context context, AttributeSet attrs) {
			super(context, attrs);
			// TODO Auto-generated constructor stub
			super.setUpdatePeriod(UPDATE_DELAY);
		}

		public OAnQuanGame(Context context) {
			super(context);
			// TODO Auto-generated constructor stub
			super.setUpdatePeriod(UPDATE_DELAY);
		}

		@Override
		protected void onDraw(Canvas canvas) {
			super.dispatchDraw(canvas);

			playGame(canvas);

		}

		private void paintPlayfield(Canvas canvas) {
			canvas.drawBitmap(background, 0, 0, mBitmapPaint);
			canvas.drawBitmap(banco, xBanCo, yBanCo, mBitmapPaint);
			canvas.drawBitmap(khungDiem, toadoKhungdiem1[0], toadoKhungdiem1[1],
					mBitmapPaint);
			canvas.drawBitmap(khungDiem, toadoKhungdiem2[0], toadoKhungdiem2[1],
					mBitmapPaint);

		}

		private void paintCoLuot(Canvas canvas) {
			if (arrayNguoiChoi[0].luotChoi == true)
				canvas.drawBitmap(coluot, toadoKhungdiem2[0] + 5,
						toadoKhungdiem2[1] + 5, mBitmapPaint);
			else if (arrayNguoiChoi[1].luotChoi == true)
				canvas.drawBitmap(coluot, toadoKhungdiem1[0] + 120,
						toadoKhungdiem1[1], mBitmapPaint);
		}

		private void paintDiem(Canvas canvas) {

			for (int i = 0; i < arrayOCo.length; i++) {
				canvas.drawText(Integer.toString(arrayOCo[i].SoHatCu),
						arrayOCo[i].toadoODiem[0], arrayOCo[i].toadoODiem[1],
						mDiemPaint);
			}

			canvas.drawText(Integer.toString(arrayNguoiChoi[1].diemSo),
					toadoKhungdiem1[0] + 40, toadoKhungdiem1[1] + 65,
					mDiemNguoiChoiPaint);
			canvas.drawText(Integer.toString(arrayNguoiChoi[0].diemSo),
					toadoKhungdiem2[0] + 40, toadoKhungdiem2[1] + 65,
					mDiemNguoiChoiPaint);

		}

		private void paintEndgame(Canvas canvas) {

			if (arrayNguoiChoi[0].diemSo > arrayNguoiChoi[1].diemSo) {
				canvas.drawBitmap(thangGame, xBanCo + 150, yBanCo + 20,
						mBitmapPaint);
				if (sound == true) {
					if (check_winsound == true) {
						winsound.play();
						check_winsound = false;
					}
				}
			} else {
				canvas
						.drawBitmap(thuaGame, xBanCo + 150, yBanCo + 20,
								mBitmapPaint);
				if (sound == true) {
					if (check_losesound == true) {
						losesound.play();
						check_losesound = false;
					}
				}
			}
		}

		private void paintHat(Canvas canvas) {
			if (quan1 == true) {
				if (arrayOCo[5].SoHatCu < 18) {
					canvas.drawBitmap(bquan1, toadoQuan1[0], toadoQuan1[1],
							mBitmapPaint);
				} else
					canvas.drawBitmap(bquan1, toadoQuan1[0], toadoQuan1[1] - 50,
							mBitmapPaint);
			}

			if (quan2 == true) {
				if (arrayOCo[11].SoHatCu < 18) {
					canvas.drawBitmap(bquan2, toadoQuan2[0], toadoQuan2[1],
							mBitmapPaint);
				} else
					canvas.drawBitmap(bquan2, toadoQuan2[0], toadoQuan2[1] + 50,
							mBitmapPaint);
			}

			for (int i = 0; i < 12; i++) {
				if (arrayOCo[i].SoHatCu > 0) {
					if ((i == 5 && quan1 == true) || (i == 11 && quan2 == true)) {
						if (arrayOCo[i].SoHatCu - 10 > 7) {
							if (i == 5) {
								canvas.drawBitmap(giau, arrayOCo[i].toadoHat[3][0],
										arrayOCo[i].toadoHat[3][1], mBitmapPaint);
							} else if (i == 11)
								canvas.drawBitmap(giau, arrayOCo[i].toadoHat[0][0],
										arrayOCo[i].toadoHat[0][1], mBitmapPaint);
						} else {
							for (int j = 0; j < arrayOCo[i].SoHatCu - 10; j++) {
								canvas.drawBitmap(soi, arrayOCo[i].toadoHat[j][0],
										arrayOCo[i].toadoHat[j][1], mBitmapPaint);
							}
						}
					}

					// nhg o khac chi xem nhu binh thuong
					else {
						if (arrayOCo[i].SoHatCu > 7) {
							if (i == 11) {
								canvas.drawBitmap(giau, arrayOCo[i].toadoHat[0][0],
										arrayOCo[i].toadoHat[0][1], mBitmapPaint);
							} else
								canvas.drawBitmap(giau, arrayOCo[i].toadoHat[2][0],
										arrayOCo[i].toadoHat[2][1], mBitmapPaint);
						} else
							for (int j = 0; j < arrayOCo[i].SoHatCu; j++) {
								canvas.drawBitmap(soi, arrayOCo[i].toadoHat[j][0],
										arrayOCo[i].toadoHat[j][1], mBitmapPaint);
							}
					}
				}

			}

		}

		public void paintSound(Canvas canvas) {
			if (sound == true) {
				canvas.drawBitmap(soundon, 770, 20, mBitmapPaint);
				bgsound.play();
			} else {
				canvas.drawBitmap(soundoff, 770, 20, mBitmapPaint);
				bgsound.stop();
			}

		}

		public void playGame(Canvas canvas) {
			paintPlayfield(canvas);
			if (gameState == CHONCAPDO) {
				canvas.drawBitmap(de, rect_de.left, rect_de.top, mBitmapPaint);
				canvas.drawBitmap(binhThuong, rect_binhthuong.left,
						rect_binhthuong.top, mBitmapPaint);
				canvas.drawBitmap(kho,rect_kho.left,rect_kho.top,mBitmapPaint);
			}
			if (gameState == BATDAU) {
				canvas.drawBitmap(batdauGame, xBanCo + 150, yBanCo + 20,
						mBitmapPaint);
			}
			if (gameState == DANGCHOI) {
				paintDiem(canvas);
				paintHat(canvas);
				paintSound(canvas);

				if (_path != null) {

					if (a < _path.size()) {

						thongTinCapNhat thongtin = _path.get(a);

						if (thongtin.trangThai == BocHat) {
							if (frame < 3) {
								canvas.drawBitmap(Bochat_sprite[frame],
										arrayOCo[thongtin.viTri].toadoHat[2][0],
										arrayOCo[thongtin.viTri].toadoHat[2][1],
										mBitmapPaint);
								frame++;
							}
							if (frame == 3) {
								frame = 0;
								arrayOCo[thongtin.viTri].SoHatCu = thongtin.soHat;
								a++;

							}

						} else if (thongtin.trangThai == DatHat) {

							if (frame < 2) {
								if (thongtin.viTri == 5 && quan1 == true
										|| thongtin.viTri == 11 && quan2 == true) {
									if (thongtin.soHat - 10 > 7) {
										canvas
												.drawBitmap(
														Dathat_sprite[frame],
														arrayOCo[thongtin.viTri].toadoHat[4][0],
														arrayOCo[thongtin.viTri].toadoHat[4][1],
														mBitmapPaint);
										frame++;
									} else {
										canvas
												.drawBitmap(
														Dathat_sprite[frame],
														arrayOCo[thongtin.viTri].toadoHat[arrayOCo[thongtin.viTri].SoHatCu - 10][0],
														arrayOCo[thongtin.viTri].toadoHat[arrayOCo[thongtin.viTri].SoHatCu - 10][1],
														mBitmapPaint);
										frame++;
									}

								} else if (thongtin.soHat > 7) {
									canvas
											.drawBitmap(
													Dathat_sprite[frame],
													arrayOCo[thongtin.viTri].toadoHat[2][0],
													arrayOCo[thongtin.viTri].toadoHat[2][1],
													mBitmapPaint);
									frame++;
								} else {
									canvas
											.drawBitmap(
													Dathat_sprite[frame],
													arrayOCo[thongtin.viTri].toadoHat[arrayOCo[thongtin.viTri].SoHatCu][0],
													arrayOCo[thongtin.viTri].toadoHat[arrayOCo[thongtin.viTri].SoHatCu][1],
													mBitmapPaint);
									frame++;
								}

							}

							if (frame == 2) {
								frame = 0;
								arrayOCo[thongtin.viTri].SoHatCu = thongtin.soHat;
								a++;

							}

						} else if (thongtin.trangThai == DapAnHat) {
							if (frame < 3) {
								canvas.drawBitmap(DapAnhat_sprite[frame],
										arrayOCo[thongtin.viTri].toadoHat[2][0],
										arrayOCo[thongtin.viTri].toadoHat[2][1],
										mBitmapPaint);
								frame++;
							}
							if (frame == 3) {
								frame = 0;
								arrayOCo[thongtin.viTri].SoHatCu = thongtin.soHat;
								a++;

							}

						} else
						// neu la an hat
						if (thongtin.trangThai == AnHat) {
							if (frame < 3) {
								canvas.drawBitmap(Bochat_sprite[frame],
										arrayOCo[thongtin.viTri].toadoHat[2][0],
										arrayOCo[thongtin.viTri].toadoHat[2][1],
										mBitmapPaint);

								frame++;
							}
							if (frame == 3) {
								frame = 0;

								if (thongtin.viTri == 5 && thongtin.soHat == 0
										&& quan1 == true) {
									canvas.drawBitmap(anquan, xBanCo + 150,
											yBanCo + 20, mBitmapPaint);
									quan1 = false;
								}

								if (thongtin.viTri == 11 && thongtin.soHat == 0
										&& quan2 == true) {
									canvas.drawBitmap(anquan, xBanCo + 150,
											yBanCo + 20, mBitmapPaint);
									quan2 = false;
								}

								arrayOCo[thongtin.viTri].SoHatCu = thongtin.soHat;
								a++;
								// cap nhat diem so
								if (arrayNguoiChoi[0].luotChoi == true) {
									arrayNguoiChoi[0].diemSo += diemAnTrongLuot;
								} else
									arrayNguoiChoi[1].diemSo += diemAnTrongLuot;
								diemAnTrongLuot = 0;

							}

						} else if (thongtin.trangThai == TuDongRaiSoi) {
							if (frame < 3) {

								canvas.drawBitmap(tuDongRaiSoi, xBanCo + 200,
										yBanCo + 50, mBitmapPaint);
								frame++;
							}
							if (frame == 3) {
								frame = 0;
								arrayOCo[thongtin.viTri].SoHatCu = thongtin.soHat;
								a++;
								// cap nhat diem so
								if (arrayNguoiChoi[0].luotChoi == true) {
									arrayNguoiChoi[1].diemSo -= diemTruTrongLuot;
								} else
									arrayNguoiChoi[0].diemSo -= diemTruTrongLuot;
								diemTruTrongLuot = 0;

							}

						} else if (thongtin.trangThai == TanQuan) {
							if (frame < 3) {

								canvas.drawBitmap(tanQuanKeoVe, xBanCo + 130,
										yBanCo + 40, mBitmapPaint);
								frame++;
							}
							if (frame == 3) {
								frame = 0;

								arrayOCo[thongtin.viTri].SoHatCu = thongtin.soHat;
								a++;
								// cap nhat diem so
								arrayNguoiChoi[0].diemSo += tanquan0;
								arrayNguoiChoi[1].diemSo += tanquan1;
								tanquan0 = 0;
								tanquan1 = 0;
							}

						} else {
							arrayOCo[thongtin.viTri].SoHatCu = thongtin.soHat;
							a++;
						}

					} else {
						// xoa cac phan tu trong arraylist
						for (int i = 0; i < _path.size(); i++) {
							_path.remove(i);
						}
						_path = null;
						a = 0;

						// da rai xong, tra bien ve true de co the tiep tuc rai

						chopheprai = true;

						// doi luot di sau khi ket thuc 1 luot
						if (arrayNguoiChoi[0].luotChoi == true) {
							arrayNguoiChoi[0].luotChoi = false;
							arrayNguoiChoi[1].luotChoi = true;
						} else {
							arrayNguoiChoi[0].luotChoi = true;
							arrayNguoiChoi[1].luotChoi = false;
						}
						if (arrayNguoiChoi[1].luotChoi == true && ketthuc == false)
							MayDi();
						// cho 1 khoang thoi gian truoc khi ve lai
						count--;
						if (count <= 0)
							count = 1000;

					}

				}

				paintCoLuot(canvas);
				if (viTriChon != -99 && arrayNguoiChoi[0].luotChoi == true) {
					canvas.drawBitmap(chonO, rect[viTriChon].left,
							rect[viTriChon].top, mBitmapPaint);
				}

				if (ketthuc == true && _path == null) {
					bgsound.stop();
					paintEndgame(canvas);
				}
			}

		}

		/*************************************************************
		 * GAME Code
		 ************************************************************/

		// audio clip
		AudioClip bgsound;
		AudioClip winsound;
		AudioClip losesound;
		AudioClip clicksound;
		boolean check_winsound = true;
		boolean check_losesound = true;
		// cac trang thai khi rai
		private static final int BocHat = 1;
		private static final int DapAnHat = 2;
		private static final int DatHat = 3;
		private static final int AnHat = 4;
		private static final int TuDongRaiSoi = 5;
		private static final int TanQuan = 6;
		// cac cap do
		private static final int DE = 1;
		private static final int BINH_THUONG = 2;
		private static final int KHO = 3;

		// cac trang thai trong game
		public static final int CHONCAPDO = 0;
		public static final int BATDAU = 1;
		public static final int DANGCHOI = 2;

		// ....
		int a = 0;
		int count = 1000;
		// cap do cua may
		int capDo;
		// bien luu lai diem an duoc hay bi tru trong moi luot cua nguoi choi
		int diemAnTrongLuot = 0;
		int diemTruTrongLuot = 0;
		// bien luu lai so tan quan cua 2 ben
		int tanquan0 ;
		int tanquan1 ;
		// xac dinh da chon o chua
		boolean daChonO ;
		// tat mo nhac nen
		boolean sound;
		// arraylist luu lai thong tin de cap nhat trong ondraw
		ArrayList<thongTinCapNhat> _path;
		// tao may
		May may = new May();
		// vi tri dung de xac dinh o hien tai
		int viTriChon;

		// bien luu cap do cua game

		// bien luu toa do cham tren man hinh
		float tx, tx1;
		float ty, ty1;
		// bien xac dinh viec cho phep thuc hien rai hat
		boolean chopheprai;
		// khung hinh
		int frame;

		// co tat ca 12 o co
		OCo[] arrayOCo = new OCo[12];
		// 2 nguoi choi
		NguoiChoi[] arrayNguoiChoi = new NguoiChoi[2];
		// back up lai luot choi cua may
		OCo[] arrayOCoBackup = new OCo[12];
		OCo[] arrayOCoBackupHard = new OCo[12];
		// 2 bien xac dinh Quan cua moi ben
		boolean quan1;
		boolean quan2;
		// boolean ve = true;

		// 2 bien luu so o hat nho trong 2 o quan lon
		// int hatQuan1;
		// int hatQuan2;

		// // Screen size
		// int width;
		// int height;
		// bien luu trang thai da cap nhat hay chua
		// boolean daCapNhat = true;
		//boolean batdau = false;
		boolean ketthuc;
		// boolean thang;
		boolean touched;
		// xac dinh co dang trong tinh trang choi game hay k
		int gameState;
		// boolean ingame = false;
		//

		// Cac bitmap su dung
		Bitmap background;
		Bitmap bquan1; // quan 1
		Bitmap bquan2; // quan 2
		Bitmap soi; // quan linh
		Bitmap giau; // nhieu soi(hon 7 vien)
		Bitmap coluot; // co luot (bao luot choi moi ben)
		Bitmap banco; // ban co de choi
		Bitmap khungDiem; // chua thong tin diem moi ben
		Bitmap anquan;
		Bitmap tuDongRaiSoi;
		Bitmap tanQuanKeoVe;
		Bitmap chonO;
		Bitmap de;
		Bitmap binhThuong;
		Bitmap kho;
		// cac sprite duoc su dung
		Bitmap[] Dathat_sprite;
		Bitmap[] Bochat_sprite;
		Bitmap[] DapAnhat_sprite;

		Bitmap thangGame;
		Bitmap thuaGame;
		Bitmap batdauGame;
		Bitmap soundon;
		Bitmap soundoff;
		// toa do ban co
		// dung toa do ban co lam toa do chuan
		float xBanCo = 130;
		float yBanCo = 150;
		// khung toa do cua nut sound
		RectF rect_sound = new RectF(760, 0, 840, 70);
		// khung toa do cua button capdo
		RectF rect_de = new RectF(xBanCo + 260, yBanCo-50, xBanCo + 350, yBanCo +40);
		RectF rect_binhthuong = new RectF(xBanCo + 180, yBanCo+50, xBanCo + 500,
				yBanCo + 140);
		RectF rect_kho=new RectF(xBanCo + 260, yBanCo + 150 , xBanCo + 350,
				yBanCo + 240);
		// khung tao do cua moi o

		RectF rect0 = new RectF(xBanCo + 470, yBanCo + 100, xBanCo + 560,
				yBanCo + 200);
		RectF rect1 = new RectF(xBanCo + 375, yBanCo + 100, xBanCo + 465,
				yBanCo + 200);
		RectF rect2 = new RectF(xBanCo + 280, yBanCo + 100, xBanCo + 370,
				yBanCo + 200);
		RectF rect3 = new RectF(xBanCo + 190, yBanCo + 100, xBanCo + 280,
				yBanCo + 200);
		RectF rect4 = new RectF(xBanCo + 95, yBanCo + 100, xBanCo + 185,
				yBanCo + 200);
		RectF[] rect = { rect0, rect1, rect2, rect3, rect4 };

		// toa do diem cac o
		float[] toadoodiem0 = { xBanCo + 545, yBanCo + 190 };
		float[] toadoodiem1 = { xBanCo + 455, yBanCo + 190 };
		float[] toadoodiem2 = { xBanCo + 360, yBanCo + 190 };
		float[] toadoodiem3 = { xBanCo + 265, yBanCo + 190 };
		float[] toadoodiem4 = { xBanCo + 175, yBanCo + 190 };
		float[] toadoodiem5 = { xBanCo + 70, yBanCo + 180 };
		float[] toadoodiem6 = { xBanCo + 175, yBanCo + 30 };
		float[] toadoodiem7 = { xBanCo + 265, yBanCo + 30 };
		float[] toadoodiem8 = { xBanCo + 360, yBanCo + 30 };
		float[] toadoodiem9 = { xBanCo + 455, yBanCo + 30 };
		float[] toadoodiem10 = { xBanCo + 545, yBanCo + 30 };
		float[] toadoodiem11 = { xBanCo + 580, yBanCo + 40 };

		// //toa do cua 2 quan to
		float[] toadoQuan1 = { xBanCo + 45, yBanCo + 65 };
		float[] toadoQuan2 = { xBanCo + 580, yBanCo + 65 };

		float[] toadoKhungdiem1 = { xBanCo + 200, yBanCo - 90 };
		float[] toadoKhungdiem2 = { xBanCo + 300, yBanCo + 216 };

		// //bang toa do cua cac hat trong o
		float[][] toado0 = { { xBanCo + 505, yBanCo + 110 },
				{ xBanCo + 530, yBanCo + 125 }, { xBanCo + 480, yBanCo + 125 },
				{ xBanCo + 530, yBanCo + 155 }, { xBanCo + 480, yBanCo + 155 },
				{ xBanCo + 505, yBanCo + 175 }, { xBanCo + 505, yBanCo + 145 } };
		float[][] toado1 = { { xBanCo + 410, yBanCo + 110 },
				{ xBanCo + 435, yBanCo + 125 }, { xBanCo + 385, yBanCo + 125 },
				{ xBanCo + 435, yBanCo + 155 }, { xBanCo + 385, yBanCo + 155 },
				{ xBanCo + 410, yBanCo + 175 }, { xBanCo + 410, yBanCo + 145 } };
		float[][] toado2 = { { xBanCo + 315, yBanCo + 110 },
				{ xBanCo + 340, yBanCo + 125 }, { xBanCo + 290, yBanCo + 125 },
				{ xBanCo + 340, yBanCo + 155 }, { xBanCo + 290, yBanCo + 155 },
				{ xBanCo + 315, yBanCo + 175 }, { xBanCo + 315, yBanCo + 145 } };
		float[][] toado3 = { { xBanCo + 220, yBanCo + 110 },
				{ xBanCo + 245, yBanCo + 125 }, { xBanCo + 195, yBanCo + 125 },
				{ xBanCo + 245, yBanCo + 155 }, { xBanCo + 195, yBanCo + 155 },
				{ xBanCo + 220, yBanCo + 175 }, { xBanCo + 220, yBanCo + 145 } };
		float[][] toado4 = { { xBanCo + 125, yBanCo + 110 },
				{ xBanCo + 150, yBanCo + 125 }, { xBanCo + 100, yBanCo + 125 },
				{ xBanCo + 150, yBanCo + 155 }, { xBanCo + 100, yBanCo + 155 },
				{ xBanCo + 125, yBanCo + 175 }, { xBanCo + 125, yBanCo + 145 } };
		float[][] toado5 = { { xBanCo + 70, yBanCo + 50 },
				{ xBanCo + 45, yBanCo + 45 }, { xBanCo + 20, yBanCo + 55 },
				{ xBanCo + 15, yBanCo + 90 }, { xBanCo + 20, yBanCo + 125 },
				{ xBanCo + 45, yBanCo + 135 }, { xBanCo + 70, yBanCo + 130 } };
		float[][] toado6 = { { xBanCo + 125, yBanCo + 10 },
				{ xBanCo + 150, yBanCo + 25 }, { xBanCo + 100, yBanCo + 25 },
				{ xBanCo + 150, yBanCo + 55 }, { xBanCo + 100, yBanCo + 55 },
				{ xBanCo + 125, yBanCo + 75 }, { xBanCo + 125, yBanCo + 45 } };
		float[][] toado7 = { { xBanCo + 220, yBanCo + 10 },
				{ xBanCo + 245, yBanCo + 25 }, { xBanCo + 195, yBanCo + 25 },
				{ xBanCo + 245, yBanCo + 55 }, { xBanCo + 195, yBanCo + 55 },
				{ xBanCo + 220, yBanCo + 75 }, { xBanCo + 220, yBanCo + 45 } };
		float[][] toado8 = { { xBanCo + 315, yBanCo + 10 },
				{ xBanCo + 340, yBanCo + 25 }, { xBanCo + 290, yBanCo + 25 },
				{ xBanCo + 340, yBanCo + 55 }, { xBanCo + 290, yBanCo + 55 },
				{ xBanCo + 315, yBanCo + 75 }, { xBanCo + 315, yBanCo + 45 } };
		float[][] toado9 = { { xBanCo + 410, yBanCo + 10 },
				{ xBanCo + 435, yBanCo + 25 }, { xBanCo + 385, yBanCo + 25 },
				{ xBanCo + 435, yBanCo + 55 }, { xBanCo + 385, yBanCo + 55 },
				{ xBanCo + 410, yBanCo + 75 }, { xBanCo + 410, yBanCo + 45 } };
		float[][] toado10 = { { xBanCo + 505, yBanCo + 10 },
				{ xBanCo + 530, yBanCo + 25 }, { xBanCo + 480, yBanCo + 25 },
				{ xBanCo + 530, yBanCo + 55 }, { xBanCo + 480, yBanCo + 55 },
				{ xBanCo + 505, yBanCo + 75 }, { xBanCo + 505, yBanCo + 45 } };
		float[][] toado11 = { { xBanCo + 565, yBanCo + 50 },
				{ xBanCo + 590, yBanCo + 35 }, { xBanCo + 620, yBanCo + 50 },
				{ xBanCo + 625, yBanCo + 85 }, { xBanCo + 620, yBanCo + 120 },
				{ xBanCo + 590, yBanCo + 130 }, { xBanCo + 565, yBanCo + 120 } };

		// cac bien tam thoi cho may

		@Override
		protected void initialize() {
			background = getImage(R.drawable.background);
			banco = getImage(R.drawable.banco);
			khungDiem = getImage(R.drawable.khungdiem);
			anquan = getImage(R.drawable.anquan);
			bquan1 = getImage(R.drawable.quan1);
			bquan2 = getImage(R.drawable.quan2);
			anquan = getImage(R.drawable.anquan);
			soi = getImage(R.drawable.soi1);
			giau = getImage(R.drawable.giau);
			coluot = getImage(R.drawable.coluot);
			Bochat_sprite = new Bitmap[3];
			Dathat_sprite = new Bitmap[2];
			DapAnhat_sprite = new Bitmap[3];
			thangGame = getImage(R.drawable.thang);
			batdauGame = getImage(R.drawable.batdau);
			thuaGame = getImage(R.drawable.thua);
			tuDongRaiSoi = getImage(R.drawable.tudongraisoi);
			tanQuanKeoVe = getImage(R.drawable.tanquankeove);
			chonO = getImage(R.drawable.chono);
			de = getImage(R.drawable.de);
			binhThuong = getImage(R.drawable.binhthuong);
			kho= getImage(R.drawable.kho);
			soundon = getImage(R.drawable.sound_on);
			soundoff = getImage(R.drawable.sound_off);

			mDiemPaint.setARGB(255, 0, 0, 255);
			mDiemPaint.setTextSize(20);
			mDiemPaint.setAntiAlias(true);
		
			mDiemNguoiChoiPaint.setARGB(255, 255, 255, 0);
			mDiemNguoiChoiPaint.setTextSize(60);

			// trang thai game luc bat dau
			gameState = CHONCAPDO;
			ketthuc = false;
			// cac audio
			bgsound = getAudioClip(R.raw.bgmusic);
			winsound = getAudioClip(R.raw.win);
			losesound = getAudioClip(R.raw.lose);
			clicksound = getAudioClip(R.raw.click);
			bgsound.stop();

			// width = getWidth();
			// height = getHeight();
			// gan sprite boc hat
			int[] ids_BocHat = new int[] { R.drawable.boc_0, R.drawable.boc_1,
					R.drawable.boc_2 };

			for (int n = 0; n < 3; n++) {
				Bochat_sprite[n] = getImage(ids_BocHat[n]);
			}
			// gan sprite dat hat
			int[] ids_DatHat = new int[] { R.drawable.rai1, R.drawable.rai2 };

			for (int n = 0; n < 2; n++) {
				Dathat_sprite[n] = getImage(ids_DatHat[n]);
			}
			// gan sprite dap an hat
			int[] ids_DapAnHat = new int[] { R.drawable.boc_1, R.drawable.dapan,
					R.drawable.boc_0 };

			for (int n = 0; n < 3; n++) {
				DapAnhat_sprite[n] = getImage(ids_DapAnHat[n]);
			}

			for (int i = 0; i < arrayOCo.length; i++)
				arrayOCo[i] = new OCo();

			// mang back up thong tin trong moi o
			for (int i = 0; i < arrayOCoBackup.length; i++)
				arrayOCoBackup[i] = new OCo();
			//mang back up cho hard thong tin trong moi o
			for (int i = 0; i < arrayOCoBackupHard.length; i++)
				arrayOCoBackupHard[i] = new OCo();
			

			for (int i = 0; i < arrayNguoiChoi.length; i++)
				arrayNguoiChoi[i] = new NguoiChoi();

		}

		public void gamestart() {

			quan1 = true;
			quan2 = true;
			arrayNguoiChoi[0].luotChoi = true;
			arrayNguoiChoi[0].diemSo = 0;
			arrayNguoiChoi[1].diemSo = 0;

			// gan so hat cu va moi cho 12 o co
			for (int i = 0; i < 12; i++) {
				if (i == 5 || i == 11) {
					arrayOCo[i].SoHatCu = 10;
					arrayOCo[i].SoHatMoi = 10;
				} else {
					arrayOCo[i].SoHatCu = 5;
					arrayOCo[i].SoHatMoi = 5;
				}
			}
			
			
			// gan toa do cho 12 o co
			arrayOCo[0].toadoHat = toado0;
			arrayOCo[1].toadoHat = toado1;
			arrayOCo[2].toadoHat = toado2;
			arrayOCo[3].toadoHat = toado3;
			arrayOCo[4].toadoHat = toado4;
			arrayOCo[5].toadoHat = toado5;
			arrayOCo[6].toadoHat = toado6;
			arrayOCo[7].toadoHat = toado7;
			arrayOCo[8].toadoHat = toado8;
			arrayOCo[9].toadoHat = toado9;
			arrayOCo[10].toadoHat = toado10;
			arrayOCo[11].toadoHat = toado11;

			// gan toa do o diem
			arrayOCo[0].toadoODiem = toadoodiem0;
			arrayOCo[1].toadoODiem = toadoodiem1;
			arrayOCo[2].toadoODiem = toadoodiem2;
			arrayOCo[3].toadoODiem = toadoodiem3;
			arrayOCo[4].toadoODiem = toadoodiem4;
			arrayOCo[5].toadoODiem = toadoodiem5;
			arrayOCo[6].toadoODiem = toadoodiem6;
			arrayOCo[7].toadoODiem = toadoodiem7;
			arrayOCo[8].toadoODiem = toadoodiem8;
			arrayOCo[9].toadoODiem = toadoodiem9;
			arrayOCo[10].toadoODiem = toadoodiem10;
			arrayOCo[11].toadoODiem = toadoodiem11;
			
			tanquan0=0;
			tanquan1=0;
			viTriChon=-99;
			frame=0;
			sound=false;
			a=0;
			daChonO=false;
			chopheprai=true;
			if(_path!=null)
				_path=null;

		}

		public void KiemTraHetQuan() {
			// kiem tra cho luot di cua nguoi tiep theo
			if (arrayNguoiChoi[0].luotChoi == true) {
				// kiem tra xem may da het quan chua, neu het roi thi rai quan
				for (int i = 6; i < 11; i++)
					if (arrayOCo[i].SoHatMoi != 0) // neu co o nao co hat thi chua
						// het
						// quan
						return;
				// neu o nao cung het quan thi rai hat
				
					for (int i = 6; i < 11; i++) {
						CapNhatHat(i, 1, TuDongRaiSoi);
						diemTruTrongLuot = 5;
					}
					

				

			} else {

				// kiem tra xem nguoi choi da het quan chua, neu het roi thi rai
				// quan

				for (int i = 0; i < 5; i++)
					if (arrayOCo[i].SoHatMoi != 0) // neu co o nao co hat thi chua
						// het quan
						return;

				// neu o nao cung het quan thi rai hat
				
				
					for (int i = 0; i < 5; i++) {
						// tu dong rai soi
						CapNhatHat(i, 1, TuDongRaiSoi);
						diemTruTrongLuot = 5;

					}
			}
		}

		public boolean KiemTraKetThuc() {
			// neu 2 o quan het quan, tan quan keo ve
			if (arrayOCo[5].SoHatMoi == 0 && arrayOCo[11].SoHatMoi == 0) {
				for (int i = 0; i < 5; i++) {
					if (arrayOCo[i].SoHatMoi > 0) {
						tanquan0 += arrayOCo[i].SoHatMoi;
						CapNhatHat(i, 0, TanQuan);
					}

				}
				for (int i = 6; i < 11; i++) {
					if (arrayOCo[i].SoHatMoi > 0) {
						tanquan1 += arrayOCo[i].SoHatMoi;
						CapNhatHat(i, 0, TanQuan);
					}

				}
				ketthuc = true;
				return true;
			}
			return false;
		}

		public void DoiLuotDi() {
			// neu dang la luot choi cua nguoi
			if (arrayNguoiChoi[0].luotChoi == true) {
				// Kiem tra game da ket thuc chua
				if (KiemTraKetThuc() == false) {
					KiemTraHetQuan();

				}

			}

			// neu dang la luot choi cua may
			else {

				if (KiemTraKetThuc() == false) {
					KiemTraHetQuan();
				}

			}
		}

		@Override
		public boolean onTouchEvent(MotionEvent event) {
			switch (event.getAction()) {
			case MotionEvent.ACTION_DOWN: {
				if (gameState == CHONCAPDO) {
					if (rect_de.contains(event.getX(), event.getY())) {
						capDo = DE;
						gameState = BATDAU;
						return true;
					} else if (rect_binhthuong.contains(event.getX(), event.getY())) {
						capDo = BINH_THUONG;
						gameState = BATDAU;
						return true;
					}
					else if(rect_kho.contains(event.getX(), event.getY())){
						capDo = KHO;
						gameState = BATDAU;
						return true;
					}
				} else if (gameState == BATDAU) {
					gamestart();
					gameState = DANGCHOI;				
				}
				else if (gameState == DANGCHOI) {

					touched = true;
					tx = event.getX();
					tx1 = tx;
					ty = event.getY();
					if (rect_sound.contains(tx, ty)) {
						if (sound == true)
							sound = false;
						else
							sound = true;
						return true;
					}

					else if (rect[0].contains(tx, ty) && arrayOCo[0].SoHatCu != 0) {
						viTriChon = 0;
						clicksound.play();
					} else if (rect[1].contains(tx, ty) && arrayOCo[1].SoHatCu != 0) {
						clicksound.play();
						viTriChon = 1;
					} else if (rect[2].contains(tx, ty) && arrayOCo[2].SoHatCu != 0) {
						clicksound.play();
						viTriChon = 2;
					} else if (rect[3].contains(tx, ty) && arrayOCo[3].SoHatCu != 0) {
						clicksound.play();
						viTriChon = 3;
					}

					else if (rect[4].contains(tx, ty) && arrayOCo[4].SoHatCu != 0) {
						clicksound.play();
						viTriChon = 4;
					}

				}

				break;
			}
			case MotionEvent.ACTION_UP:
				clicksound.stop();
				if (touched == true) {
					tx1 = event.getX();
					if (tx1 < tx - 100) {
						if (viTriChon != -99)
							if (chopheprai == true)
								RaiBenTrai(viTriChon);
					} else if (tx1 > tx + 100) {
						if (viTriChon != -99)

							if (chopheprai == true)
								RaiBenPhai(viTriChon);
					}
				}
				touched = false;
				break;
			}
			return true;
		}



		public void CapNhatHat(int viTri, int sohat, int trangThai) {

			if (_path == null) {
				_path = new ArrayList<thongTinCapNhat>();
			}
			_path.add(new thongTinCapNhat(viTri, sohat, trangThai));
			arrayOCo[viTri].SoHatMoi = sohat;

		}

		public void RaiBenTrai(int vitri) {
			// chuyen co ve false
			chopheprai = false;
			viTriChon = -99;
			// kiem tra so hat o duoc chon co lon hon 0 hay k
			if (arrayOCo[vitri].SoHatMoi == 0)
				return;
			int soHat = arrayOCo[vitri].SoHatMoi;

			// chuyen so hat o o vua boc tro ve 0
			CapNhatHat(vitri, 0, BocHat);

			// bat dau rai hat
			while (soHat >= 0) {
				vitri++;
				if (vitri > 11)
					vitri = 0;
				// truong hop so hat de rai da het va o tiep theo van con hat
				if (soHat == 0 && arrayOCo[vitri].SoHatMoi != 0) {
					// o tiep theo con hat la o quan
					if (vitri == 5 || vitri == 11) {
						DoiLuotDi();
						break;
					} else
						// boc hat tai o do
						soHat = arrayOCo[vitri].SoHatMoi;
					CapNhatHat(vitri, 0, BocHat);

					vitri++;
					if (vitri > 11)
						vitri = 0;
				}

				// truong hop het hat va gap o trong
				else if (soHat == 0 && arrayOCo[vitri].SoHatMoi == 0) {
					int vitrike;
					vitrike = vitri + 1;
					if (vitrike > 11)
						vitrike = 0;
					// neu o do trong va o ke tiep co hat thi an
					while (arrayOCo[vitri].SoHatMoi == 0
							&& arrayOCo[vitrike].SoHatMoi != 0) {
						if (vitrike > 11)
							vitrike = 0;
						// neu o trong la o quan, k an j ca
						if (vitri == 5 || vitri == 11) {
							DoiLuotDi();
							return;
						}
						// neu o trong chi la o binh thuong, an o do va xet o tiep
						// theo
						else {

							diemAnTrongLuot += arrayOCo[vitrike].SoHatMoi;
							// an hat tai o nay
							CapNhatHat(vitri, 0, DapAnHat);
							CapNhatHat(vitrike, 0, AnHat);

							vitri += 2;
							if (vitri > 11)
								vitri = 0;
							vitrike = vitri + 1;
							if (vitrike > 11)
								vitrike = 0;

						}

					}

					DoiLuotDi();
					return;
				}
				CapNhatHat(vitri, arrayOCo[vitri].SoHatMoi + 1, DatHat);

				soHat--;
			}

		}

		public void RaiBenPhai(int vitri) {
			// chuyen co
			chopheprai = false;
			viTriChon = -99;
			// kiem tra so hat o duoc chon co lon hon 0 hay k
			if (arrayOCo[vitri].SoHatMoi == 0)
				return;
			int soHat = arrayOCo[vitri].SoHatMoi;
			// chuyen so hat o o vua boc tro ve 0
			CapNhatHat(vitri, 0, BocHat);
			// bat dau rai hat

			while (soHat >= 0) {
				vitri--;
				if (vitri < 0)
					vitri = 11;
				// truong hop so hat de rai da het va o tiep theo van con hat
				if (soHat == 0 && arrayOCo[vitri].SoHatMoi != 0) {
					// o tiep theo con hat la o quan
					if (vitri == 5 || vitri == 11) {
						DoiLuotDi();
						break;
					} else
						// boc hat tai o do
						soHat = arrayOCo[vitri].SoHatMoi;
					CapNhatHat(vitri, 0, BocHat);
					vitri--;
					if (vitri < 0)
						vitri = 11;

				}

				// truong hop gap o trong
				else if (soHat == 0 && arrayOCo[vitri].SoHatMoi == 0) {
					int vitrike;
					vitrike = vitri - 1;
					if (vitrike == -1)
						vitrike = 11;

					while (arrayOCo[vitri].SoHatMoi == 0
							&& arrayOCo[vitrike].SoHatMoi != 0) {
						// neu o trong la o quan, k an j ca
						if (vitri == 5 || vitri == 11) {
							DoiLuotDi();
							return;
						} else {

							diemAnTrongLuot += arrayOCo[vitrike].SoHatMoi;
							CapNhatHat(vitri, 0, DapAnHat);
							CapNhatHat(vitrike, 0, AnHat);
							vitri -= 2;
							if (vitri == -1)
								vitri = 11;
							if (vitri == -2)
								vitri = 10;
							vitrike = vitri - 1;
							if (vitrike == -1)
								vitrike = 11;

						}

					}
					DoiLuotDi();
					return;
				}
				CapNhatHat(vitri, arrayOCo[vitri].SoHatMoi + 1, DatHat);
				soHat--;

			}

		}

		// cac ham AI cho may
		public void DanhGiaNuocDi_Easy() {
			for (int i = 6; i < 11; i++) {
				if (arrayOCo[i].SoHatMoi > 0) {
					may.viTriMayChon = i; // co hat thi chon
					if (i % 2 == 0) // neu o chon la chan thi rai ben trai
						may.huongMayChon = 0;
					else
						may.huongMayChon = 1;
					return;
				}
			}
		}

		public void DanhGiaNuocDi_Medium() {

			may.soHatTamThoi = 0;
			may.soHatMax = -1;
			may.viTriMayChon = 6;
			may.huongMayChon = 0;
			// cho may rai quan lan luot ve 2 ben de xem khi rai o o nao thi so hat
			// an duoc la lon nhat
			for (int i = 6; i < 11; i++) {
				may.soHatTamThoi = May_RaiBenTrai(i);
				if (may.soHatTamThoi > may.soHatMax) {
					may.soHatMax = may.soHatTamThoi;
					may.viTriMayChon = i;
					may.huongMayChon = 0;
				}
				// tra lai trang thai ban dau sau khi may thuc hien 1 nuoc di thu
				for (int j = 0; j < 12; j++)
					arrayOCo[j].SoHatCu = arrayOCoBackup[j].SoHatCu;
			}

			for (int i = 6; i < 11; i++) {
				may.soHatTamThoi = May_RaiBenPhai(i);
				if (may.soHatTamThoi > may.soHatMax) {
					may.soHatMax = may.soHatTamThoi;
					may.viTriMayChon = i;
					may.huongMayChon = 1;
				}
				// tra lai trang thai ban dau sau khi may thuc hien di thu
				for (int j = 0; j < 12; j++)
					arrayOCo[j].SoHatCu = arrayOCoBackup[j].SoHatCu;
			}
		}
		
		public void DanhGiaNuocDi_Hard()
	    {
	        
	        // may se thuc hien di thu, ung voi moi lan di thu, may se tiep tuc di thu luon luot cua nguoi choi de tim duong di tot nhat
	        may.soHatMax = 0;
	        may.huongMayChon = 0;
	        int hieusomin=100; //truong hop may rai an 0 va ng k co hat de rai
	        int hieuso=0;
	        int soHatNguoiChoiAnTamThoi=0;
	        int soHatNguoiChoiAnMax;
	        //cho may rai quan lan luot ve 2 ben de xem khi rai o o nao thi so hat an duoc la lon nhat
	        for (int i = 6; i < 11; i++)
	        {
	            may.soHatMax = May_RaiBenTrai(i);
	            //neu o do co hat
	            if (may.soHatMax != -1)
	            {         
	                //backup lai de rai luot cua nguoi
	                for (int m = 0; m < 12; m++)
	                    arrayOCoBackupHard[m].SoHatCu = arrayOCo[m].SoHatCu;
	                //may lan luot di thu cac nuoc cua nguoi choi sau khi da thuc hien
	                //rai thu nuoc di cua minh, tim ra nuoc di an nhieu nhat
	                //ung voi lan rai thu i cua may
	                soHatNguoiChoiAnMax = -1;
	                soHatNguoiChoiAnTamThoi = 0;
	                for (int k = 0; k < 5; k++)
	                {
	                    soHatNguoiChoiAnTamThoi = May_RaiBenTrai(k);
	                    if (soHatNguoiChoiAnTamThoi > soHatNguoiChoiAnMax)
	                        soHatNguoiChoiAnMax = soHatNguoiChoiAnTamThoi;

	                    //back up lai sau moi lan rai thu cua nguoi
	                    for (int j = 0; j < 12; j++)
	                        arrayOCo[j].SoHatCu = arrayOCoBackupHard[j].SoHatCu;
	                }
	                for (int k = 0; k < 5; k++)
	                {                   
	                    soHatNguoiChoiAnTamThoi = May_RaiBenPhai(k);
	                    if (soHatNguoiChoiAnTamThoi > soHatNguoiChoiAnMax)
	                        soHatNguoiChoiAnMax = soHatNguoiChoiAnTamThoi;
	                    //backup lai sau moi lan rai thu cua nguoi
	                    for (int j = 0; j < 12; j++)
	                        arrayOCo[j].SoHatCu = arrayOCoBackupHard[j].SoHatCu;
	                }
	                
	                //xet xem giua nguoi voi may ai an nhieu hon sau lan rai thu
	                hieuso = soHatNguoiChoiAnMax - may.soHatMax;
	                if (hieusomin >= hieuso)
	                {
	                    //luu lai neu nuoc di do la tot nhat
	                    hieusomin = hieuso;
	                    may.viTriMayChon = i;
	                    may.huongMayChon = 0;
	                }

	                //tra lai trang thai ban dau sau khi may thuc hien 1 nuoc di thu
	                for (int j = 0; j < 12; j++)
	                    arrayOCo[j].SoHatCu = arrayOCoBackup[j].SoHatCu;
	            }
	         }

	         for (int i = 6; i < 11; i++)
	         {
	             may.soHatMax = May_RaiBenPhai(i);
	             //neu o do co hat
	             if (may.soHatMax != -1)
	             {

	                 //backup lai de rai luot cua nguoi
	                 for (int m = 0; m < 12; m++)
	                     arrayOCoBackupHard[m].SoHatCu = arrayOCo[m].SoHatCu;
	                 //may lan luot di thu cac nuoc cua nguoi choi sau khi da thuc hien
	                 //rai thu nuoc di cua minh, tim ra nuoc di an nhieu nhat
	                 //ung voi lan rai thu i cua may
	                 soHatNguoiChoiAnMax = -1;
	                 soHatNguoiChoiAnTamThoi = 0;
	                 for (int k = 0; k < 5; k++)
	                 {
	                     soHatNguoiChoiAnTamThoi = May_RaiBenTrai(k);
	                     if (soHatNguoiChoiAnTamThoi > soHatNguoiChoiAnMax)
	                         soHatNguoiChoiAnMax = soHatNguoiChoiAnTamThoi;
	                     //back up lai sau moi lan rai thu cua nguoi
	                     for (int j = 0; j < 12; j++)
	                         arrayOCo[j].SoHatCu = arrayOCoBackupHard[j].SoHatCu;
	                 }
	                 for (int k = 0; k < 5; k++)
	                 {
	                     //for (int m = 0; m < 12; m++)
	                       //  arrayOQuan[m].DemSoHat = arrayOquanBackup[m].DemSoHat;

	                     soHatNguoiChoiAnTamThoi = May_RaiBenPhai(k);
	                     if (soHatNguoiChoiAnTamThoi > soHatNguoiChoiAnMax)
	                         soHatNguoiChoiAnMax = soHatNguoiChoiAnTamThoi;
	                     //backup lai sau moi lan rai thu cua nguoi
	                     for (int j = 0; j < 12; j++)
	                         arrayOCo[j].SoHatCu = arrayOCoBackupHard[j].SoHatCu;
	                 }

	                 hieuso = soHatNguoiChoiAnMax - may.soHatMax;

	                 if (hieusomin >= hieuso)
	                 {
	                     hieusomin = hieuso;
	                     may.viTriMayChon = i;
	                     may.huongMayChon = 0;
	                 }
	                
	                 //tra lai trang thai ban dau sau khi may thuc hien 1 nuoc di thu
	                 for (int j = 0; j < 12; j++)
	                     arrayOCo[j].SoHatCu = arrayOCoBackup[j].SoHatCu;
	             }
	         } 
	    }

		// ham de may xem xet khi thuc hien rai ben trai
		public int May_RaiBenTrai(int vitri) {
			int soHatAnDuoc = 0;
			// kiem tra so hat o duoc chon co lon hon 0 hay k
			if (arrayOCo[vitri].SoHatCu == 0)
				return -1;
			int soHat = arrayOCo[vitri].SoHatCu;
			// chuyen so hat o o vua boc tro ve 0
			arrayOCo[vitri].SoHatCu = 0;
			// bat dau rai hat
			while (soHat >= 0) {
				vitri++;
				if (vitri > 11)
					vitri = 0;
				// truong hop so hat de rai da het va o tiep theo van con hat
				if (soHat == 0 && arrayOCo[vitri].SoHatCu != 0) {
					// o tiep theo con hat la o quan
					if (vitri == 5 || vitri == 11) {
						// DoiLuotDi();
						return soHatAnDuoc;
					} else
						// boc hat tai o do
						soHat = arrayOCo[vitri].SoHatCu;
					// dua so hat tai vi tri do ve 0
					arrayOCo[vitri].SoHatCu = 0;
					vitri++;
					if (vitri > 11)
						vitri = 0;
				}
				// truong hop gap o trong
				else if (soHat == 0 && arrayOCo[vitri].SoHatCu == 0) {
					int vitrike;
					vitrike = vitri + 1;
					if (vitrike > 11)
						vitrike = 0;
					while (arrayOCo[vitri].SoHatCu == 0
							&& arrayOCo[vitrike].SoHatCu != 0) {
						// neu o trong la o quan, k an j ca
						if (vitri == 5 || vitri == 11)
							return soHatAnDuoc;
						else {
							soHatAnDuoc += arrayOCo[vitrike].SoHatCu;
							// sau khi an, dua hat o do ve 0
							arrayOCo[vitrike].SoHatCu = 0;
							// tang len de tiep tu xet an combo
							vitri += 2;
							if (vitri > 11)
								vitri = 0;
							vitrike = vitri + 1;
							if (vitrike > 11)
								vitrike = 0;
						}
					}
					return soHatAnDuoc;
				}
				arrayOCo[vitri].SoHatCu += 1;
				soHat--;
			}
			return soHatAnDuoc;

		}

		// Ham cho may thuc hien kiem tra viec rai ben phai
		public int May_RaiBenPhai(int vitri) {
			int soHatAnDuoc = 0;
			// kiem tra so hat o duoc chon co lon hon 0 hay k
			if (arrayOCo[vitri].SoHatCu == 0)
				return -1;
			int soHat = arrayOCo[vitri].SoHatCu;
			// chuyen so hat o o vua boc tro ve 0
			arrayOCo[vitri].SoHatCu = 0;
			// bat dau rai hat
			while (soHat >= 0) {
				vitri--;
				if (vitri < 0)
					vitri = 11;
				// truong hop so hat de rai da het va o tiep theo van con hat
				if (soHat == 0 && arrayOCo[vitri].SoHatCu != 0) {
					// o tiep theo con hat la o quan
					if (vitri == 5 || vitri == 11)
						return soHatAnDuoc;
					else
						// boc hat tai o do
						soHat = arrayOCo[vitri].SoHatCu;
					arrayOCo[vitri].SoHatCu = 0;
					vitri--;
					if (vitri < 0)
						vitri = 11;
				}
				// truong hop gap o trong
				else if (soHat == 0 && arrayOCo[vitri].SoHatCu == 0) {
					int vitrike;
					vitrike = vitri - 1;
					if (vitrike == -1)
						vitrike = 11;
					while (arrayOCo[vitri].SoHatCu == 0
							&& arrayOCo[vitrike].SoHatCu != 0) {

						if (vitrike < 0)
							vitrike = 11;
						// neu o trong la o quan, k an j ca
						if (vitri == 5 || vitri == 11)
							return soHatAnDuoc;
						else {
							soHatAnDuoc += arrayOCo[vitrike].SoHatCu;
							arrayOCo[vitrike].SoHatCu = 0;
							vitri -= 2;
							if (vitri == -1)
								vitri = 11;
							if (vitri == -2)
								vitri = 10;
							vitrike = vitri - 1;
							if (vitrike == -1)
								vitrike = 11;

						}
					}
					return soHatAnDuoc;
				}
				arrayOCo[vitri].SoHatCu += 1;
				soHat--;
			}
			return soHatAnDuoc;
		}

		public void MayDi() {
			// luu lai so hat trong moi o truoc khi cho may chay thu
			 for (int i = 0; i < 12; i++)
			 arrayOCoBackup[i].SoHatCu = arrayOCo[i].SoHatCu;

			// Chon nuoc di
			if (capDo == DE)
				DanhGiaNuocDi_Easy();
			else if (capDo == BINH_THUONG)
				DanhGiaNuocDi_Medium();
			else if(capDo==KHO)
				DanhGiaNuocDi_Hard();

			// Thuc hien rai quan
			switch (may.viTriMayChon) {
			case 6:
				if (may.huongMayChon == 0)
					RaiBenTrai(6);
				else
					RaiBenPhai(6);
				break;
			case 7:
				if (may.huongMayChon == 0)
					RaiBenTrai(7);
				else
					RaiBenPhai(7);
				break;
			case 8:
				if (may.huongMayChon == 0)
					RaiBenTrai(8);
				else
					RaiBenPhai(8);
				break;
			case 9:
				if (may.huongMayChon == 0)
					RaiBenTrai(9);
				else
					RaiBenPhai(9);
				break;
			case 10:
				if (may.huongMayChon == 0)
					RaiBenTrai(10);
				else
					RaiBenPhai(10);
				break;
			}

		}

}
